package rpg.util;

/**
 *
 * @author Rothens
 */
public class Vector {

    private static final double EPSILON = 0.000000001;
    private double x;
    private double y;

    public Vector() {
        this(0, 0);
    }

    public Vector(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Vector(Vector v) {
        this(v.x, v.y);
    }

    public Vector normalize(){
        return this.div(length());
    }
    
    public void setX(double x) {
        this.x = x;
    }

    public double getX() {
        return x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getY() {
        return y;
    }

    public Vector add(Vector v) {
        x += v.x;
        y += v.y;
        return this;
    }

    public Vector sub(Vector v) {
        x -= v.x;
        y -= v.y;
        return this;
    }

    public Vector mul(double amt) {
        x *= amt;
        y *= amt;
        return this;
    }

    public Vector div(double amt) {
        x /= amt;
        y /= amt;
        return this;
    }
    
    public Vector normal(){
        return new Vector(-y, x);
    }

    public double dot(Vector other) {
        return (x * other.x + y * other.y);
    }

    public double lengthSq() {
        return x * x + y * y;
    }

    public double length() {
        return Math.sqrt(lengthSq());
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Vector) {
            Vector v = (Vector) o;
            return Math.abs(x - v.x) < EPSILON && Math.abs(y - v.y) < EPSILON;
        }
        return false;
    }

    public double distanceSq(Vector other){
        return Math.pow(x-other.x, 2)+Math.pow(y-other.y, 2);
    }
    
    public double distance(Vector other){
        return Math.sqrt(distanceSq(other));
    }
    
    @Override
    public int hashCode() {
        int hash = 3;
        hash = 23 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
        hash = 23 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
        return hash;
    }

    @Override
    public String toString() {
        return String.format("[%.2f;%.2f]", x,y);
    }
    
    public boolean hasNaN(){
        return x == Double.NaN || y == Double.NaN;
    }
    
    
}
